package com.hotel.front;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.SharedCache;
import com.StatusTitle;
import com.Util;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.gson.Gson;
import com.hotel.BL.FloorLogic;
import com.hotel.BL.GenericLogic;
import com.hotel.BL.RoomLogic;

import data.CleaningStatus;
import data.Floor;
import data.passingData.FloorData;
import data.passingData.ResultData;

@SuppressWarnings("serial")
public class FloorService extends HttpServlet {
	//http://localhost:8080/floorService?func=addFloor&title=FloorA
	//http://localhost:8080/floorService?func=editFloor&floorId=828&title=FloorAB
	//http://localhost:8080/floorService?func=delFloor&floorId=828
	//http://localhost:8080/floorService?func=changeFloor&floorId=828
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try{
			String doClass = req.getParameter("func");
			if(doClass.equals("addFloor")){
				AddFloor(req,resp);
			}else if(doClass.equals("editFloor")){
				EditFloor(req,resp);
			}else if(doClass.equals("delFloor")){
				DeleteFloor(req,resp);
			}else if(doClass.equals("changeFloor")){
				ChangeStatus(req,resp);
			}else if(doClass.equals("getFloor4Admin")){
				GetFloor4Admin(req,resp);
			}else if(doClass.equals("getFloorById")){
				GetFloorById(req,resp);
			}else if(doClass.equals("getAllFloor")){
				GetAllFloor(req,resp);
			}else if(doClass.equals("addRoom")){
				AddRoom(req,resp);
			}else if(doClass.equals("changeTrue")){
				ChangeTrue(req,resp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	

	private void AddFloor(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{		
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}	
		long hotelId = (Long) session.getAttribute("hotelId");
		String title = req.getParameter("title");

		new FloorLogic(req,resp).AddFloor(hotelId,title);

		GetFloor4Admin(req, resp);
	}

	private void EditFloor(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long floorId = Long.parseLong(req.getParameter("id"));
		String title = req.getParameter("title");
		
		new FloorLogic(req,resp).EditFloor(floorId,title);

		GetFloor4Admin(req, resp);
	}

	private void DeleteFloor(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long hotelId = (Long) session.getAttribute("hotelId");
		String floorIdList = req.getParameter("floorId");
		long roomamount = 0;
		List<String> list = new ArrayList<String>();
		FloorLogic floorLogic = new FloorLogic(req,resp);
		RoomLogic roomLogic = new RoomLogic(req,resp);
		for(int i = 0; i < floorIdList.split(",").length ; i++){
			long roomsize = roomLogic.GetRoomByFloorId(hotelId, Long.parseLong(floorIdList.split(",")[i]));
			roomamount += roomsize;
			if(roomsize !=0){
				list.add(floorLogic.GetFloor(Long.parseLong(floorIdList.split(",")[i])).getTitle());
			}
		}
		if(roomamount == 0){
			for(int i=0;i<floorIdList.split(",").length ; i++){
				floorLogic.DeleteFloor(Long.parseLong(floorIdList.split(",")[i]));
			}
			GetFloor4Admin(req, resp);
		}else{
			
			HashMap<String,Object> returndata = new HashMap<String,Object>();
			returndata.put("data",null);
			returndata.put("cantdelete",list);
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(returndata));
		}
	}

	private void ChangeStatus(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long hotelId = (Long) session.getAttribute("hotelId");
		long floorId = Long.parseLong(req.getParameter("floorId"));
		FloorLogic floorLogic = new FloorLogic(req,resp);
		
		boolean isFoundUsed = floorLogic.CanChangeFloor(hotelId,floorId);
		HashMap<String,Object> data = new HashMap<String,Object>();
		if(isFoundUsed){
			data.put("data", floorLogic.GetFloor(floorId));
			data.put("canchange", false);
		}else{
			data.put("data", floorLogic.ChangeStatus(floorId));
			data.put("canchange", true);
		}
		
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(data));
	}
	
	private void GetFloor4Admin(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);
		long hotelId = (Long) session.getAttribute("hotelId");
		long offset = Long.parseLong(req.getParameter("offset"));
		long line = Long.parseLong(req.getParameter("line"));
		long pointer = Long.parseLong(req.getParameter("pagePointer"));
		FloorLogic floorLogic = new FloorLogic(req,resp);
		
		List<FloorData> obj = floorLogic.GetAllFloor4AdminLimit(hotelId,offset,line);
		double dataSize = floorLogic.GetAllFloor4Admin(hotelId).size();
		
		Gson gson = new Gson();
		
		HashMap<String,Object> data = new HashMap<String,Object>();
		data.put("dataAmount",dataSize);
		data.put("pageNumber",pointer);
		data.put("lineAmount", line);
		data.put("pageAmount", Math.ceil(dataSize/line));
		data.put("data", obj);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(data));
	}
	
	private void GetFloorById(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		long id = Long.parseLong(req.getParameter("id"));
		Floor obj = new FloorLogic(req,resp).GetFloor(id);
		Gson gson = new Gson();

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void GetAllFloor(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{
		HttpSession session = req.getSession(true);
		long hotelId = 1;
		if (session.getAttribute("hotelId") != null)
			hotelId = (Long) (session.getAttribute("hotelId"));
		List<Floor> obj = new FloorLogic(req,resp).GetAllFloor(hotelId);
		Gson gson = new Gson();

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void AddRoom(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{		
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}	
		long hotelId = (Long) session.getAttribute("hotelId");
		
		String roomName = req.getParameter("roomname");
		String roomtypeString = req.getParameter("roomtype");
		String floorString = req.getParameter("floor");
		String blockString = req.getParameter("block");
		String comment = req.getParameter("description");
		CleaningStatus a = new GenericLogic(req,resp).GetCleaningStatus(hotelId,StatusTitle.CleaningStatusTitle.CleanRooms);
		String cleaningStatusString = String.valueOf(a.getId());

		new RoomLogic(req,resp).AddRoom(hotelId,roomName,roomtypeString,floorString,blockString,comment,cleaningStatusString,0);
		SharedCache.clearHotelCacheStartWith(hotelId,"RoomLogic");

		GetFloor4Admin(req, resp);
	}
	
	private void ChangeTrue(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}	
		long hotelId = (Long) session.getAttribute("hotelId");
		FloorLogic floorLogic= new FloorLogic(req,resp);
		List<Floor> list = floorLogic.GetAllFloorInHotel(hotelId);
		for(int i=0;i<list.size();i++){
			Floor x = list.get(i);
			x.setAvaliable(true);
			x.setStatus(true);
			floorLogic.UpdateFloor(x);
		}

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(true));
	}
}
